#include <iostream>
#include <vector>
#include <stack>
using namespace std;

class segment_tree {
	vector<int> pre;
	vector<int> suf;

	void up(int i, int ln, int rn)
	{
		pre[i] = pre[i << 1] == ln ? pre[i << 1] + pre[i << 1 | 1] : pre[i << 1];
		suf[i] = suf[i << 1 | 1] == rn ? suf[i << 1 | 1] + suf[i << 1] : suf[i << 1 | 1];
	}

	void build(int l, int r, int i)
	{
		if (l == r) {
			pre[i] = suf[i] = 1;
		} else {
			int mid = (l + r) >> 1;
			build(l, mid, i << 1);
			build(mid + 1, r, i << 1 | 1);
			up(i, mid - l + 1, r - mid);
		}
	}

    public:
	segment_tree(int n) : pre(n << 2, 1), suf(n << 2, 1)
	{
		build(1, n, 1);
	}

	void update(int jobi, int v, int l, int r, int i)
	{
		if (l == r) {
			pre[i] = suf[i] = v;
		} else {
			int mid = (l + r) >> 1;
			if (jobi <= mid) {
				update(jobi, v, l, mid, i << 1);
			} else {
				update(jobi, v, mid + 1, r, i << 1 | 1);
			}
			up(i, mid - l + 1, r - mid);
		}
	}

	int query(int jobi, int l, int r, int i)
	{
		if (l == r) {
			return pre[i];
		} else {
			int mid = (l + r) >> 1;
			if (jobi <= mid) {
				if (jobi < mid - suf[i << 1] + 1) {
					return query(jobi, l, mid, i << 1);
				} else {
					return suf[i << 1] + pre[i << 1 | 1];
				}
			} else {
				if (jobi > mid + pre[i << 1 | 1]) {
					return query(jobi, mid + 1, r, i << 1 | 1);
				} else {
					return suf[i << 1] + pre[i << 1 | 1];
				}
			}
		}
	}
};

int main()
{
	int n, m;
	cin >> n >> m;

	stack<int> st;
	segment_tree sgt(n);
	char op;
	for (int i = 0, pos; i < m; i++) {
		cin >> op;
		if (op == 'D') {
			cin >> pos;
			st.push(pos);
			sgt.update(pos, 0, 1, n, 1);
		} else if (op == 'R') {
			pos = st.top();
			st.pop();
			sgt.update(pos, 1, 1, n, 1);
		} else {
			cin >> pos;
			cout << sgt.query(pos, 1, n, 1) << endl;
		}
	}
	return 0;
}